import matplotlib.pyplot as plt
from BigDataWeb.algorithm import Algorithm


# 聚类算法(父类)
class Clusterer(Algorithm):
    # 类别列表
    label_list = []
    # 簇的数目
    cluster_count = 0
        
    def saveToExcle(self):
        self.data_source["_类别"] = self.label_list
        Algorithm.saveToExcle(self)
    
    def drawChart(self):
        # 类别计数
        _labels = []
        _sizes = []
        _counts = self.data_source["_类别"].value_counts()
        for label in _counts.index:
            _labels.append("类别%d(%d)" % (label, _counts[label]))
            _sizes.append(_counts[label])
        # 绘制饼图
        fig = plt.figure()
        ax = fig.add_subplot(111, aspect=1)
        fig.add_axes(aspect=1)
        ax.pie(x=_sizes, labels=_labels, autopct='%1.1f%%')
        fig.savefig("%s/pie.png" % (self.chart_path))
        # 绘制散点图
        if len(self.input_field_names) == 2:
            fig = plt.figure()
            ax = fig.add_subplot(111)
            ax.scatter(self.inputs[:, 0], self.inputs[:, 1], c=self.label_list)
            ax.set_xlabel(self.input_field_names[0])
            ax.set_ylabel(self.input_field_names[1])
            fig.savefig("%s/scatter.png" % (self.chart_path))
